package bash

import (
	"errors"
	"time"

	"github.com/astaxie/beego/cache"
	"github.com/astaxie/beego/logs"
	"github.com/qiangmzsx/beegofreecache"
)

// 设置缓存
// 名称,值,时间
func CacheSet(name string, value interface{}, times time.Duration) bool {
	var bm, err = cache.NewCache("beegofreecache", `{"size":64}`)
	if err != nil {
		return false
	}
	err = bm.Put(name, value, times)
	if err != nil {
		logs.Error("缓存存储失败，原因：" + err.Error())
		return false
	}
	return true
}

// 判断缓存是否存在
func CacheCheck(name string) bool {
	var bm, err = cache.NewCache("beegofreecache", `{"size":64}`)
	if err != nil {
		return false
	}
	return bm.IsExist(name)
}

// 读取缓存中的值
// 需要使用beegofreecache.GobDecode(val.([]byte), &value)来格式化返回的值
func CacheGet(name string) (interface{}, error) {
	var bm, err = cache.NewCache("beegofreecache", `{"size":64}`)
	if err != nil {
		return nil, err
	}
	if !bm.IsExist(name) {
		return nil, errors.New("缓存 " + name + " 不存在")
	}
	return bm.Get(name), nil
}

// 获取字符串格式的缓存
func CacheGetString(name string) string {
	val, err := CacheGet(name)
	if err != nil {
		return ""
	}
	var vals string
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return ""
	}
	return vals
}

// 获取UINT格式的缓存
func CacheGetUint(name string) uint {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals uint
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取uint64格式的缓存
func CacheGetUint64(name string) uint64 {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals uint64
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取uint32格式的缓存
func CacheGetUint32(name string) uint32 {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals uint32
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取uint8格式的缓存
func CacheGetUint8(name string) uint8 {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals uint8
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取int格式的缓存
func CacheGetInt(name string) int {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals int
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取int64格式的缓存
func CacheGetInt64(name string) int64 {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals int64
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取int32格式的缓存
func CacheGetInt32(name string) int32 {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals int32
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取int8格式的缓存
func CacheGetInt8(name string) int8 {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals int8
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取float32格式的缓存
func CacheGetFloat32(name string) float32 {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals float32
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 获取float64格式的缓存
func CacheGetFloat64(name string) float64 {
	val, err := CacheGet(name)
	if err != nil {
		return 0
	}
	var vals float64
	err = beegofreecache.GobDecode(val.([]byte), &vals)
	if err != nil {
		return 0
	}
	return vals
}

// 删除缓存信息
func CacheDelete(name string) bool {
	var bm, err = cache.NewCache("beegofreecache", `{"size":64}`)
	if err != nil {
		return false
	}
	err = bm.Delete(name)
	if err != nil {
		return false
	}
	return true
}
